package com.bt.lms.ui.action.school_education.stu_basic.BasicMes;

import com.bt.lms.ui.page.school_education.stu_basic.Nation;
import com.bt.lms.ui.page.sys.style.BtnStyle;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;

import java.io.File;

/**
 * 学员信息管理里的编辑按钮功能事件
 */
public class BasicMesEditAction implements EventHandler {
    private Integer baseWidth = 1230;

    private Integer hBoxWidth = 1230 - 300;

    private VBox totalVBox;

    private HBox one;

    public static  File file;

    private static  VBox basicVBox;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public BasicMesEditAction(VBox totalVBox, HBox one,VBox vBox) {
        this.totalVBox = totalVBox;
        this.one = one;
        this.basicVBox = vBox;
    }

    @Override
    public void handle(Event event) {

        HBox btnBox = new HBox();
        btnBox.setPrefWidth(1230);
        btnBox.setAlignment(Pos.CENTER_RIGHT);
        btnBox.setSpacing(10);
        Insets insets1 = new Insets(10,30,10,10);
        btnBox.setPadding(insets1);
        Button saveBtn = new Button("保存");
        Button cancelBtn = new Button("取消");
        btnBox.getChildren().add(saveBtn);
        btnBox.getChildren().add(cancelBtn);
        BtnStyle.saveBtn(saveBtn);
        BtnStyle.cancelBtn(cancelBtn);
        ObservableList<Node> children = totalVBox.getChildren();
        children.set(2,btnBox);

        //保存验证
        EditBasicMesVerify editBasicMesVerify = new EditBasicMesVerify(this.totalVBox);
        saveBtn.setOnAction(editBasicMesVerify);
        saveBtn.setCursor(Cursor.HAND);
        cancelBtn.setCursor(Cursor.HAND);


        VBox vBox1 = new VBox();

        HBox totalBox = new HBox();
        totalBox.setSpacing(10);
        totalBox.setPadding(new Insets(10));
        totalBox.setPrefWidth(baseWidth);

        VBox oneVbox = new VBox();
        oneVbox.setPrefWidth(hBoxWidth);
        oneVbox.setSpacing(20);
        totalBox.getChildren().add(oneVbox);
        //内容盒子
        Integer width = hBoxWidth/8;
        Integer widthMes = hBoxWidth/3;

        HBox noBox = new HBox();
        noBox.setSpacing(10);
        Label noLab = new Label("学员编号");
        noLab.setPrefWidth(width);
        TextField noTF = new TextField();
        noTF.setPrefWidth(widthMes);
        noTF.setId("noTF");
        Label nameLab = new Label("姓名");
        nameLab.setPrefWidth(width);
        TextField nameTF = new TextField();
        nameTF.setPrefWidth(widthMes);
        nameTF.setId("nameTF");
        noBox.getChildren().addAll(noLab,noTF,nameLab,nameTF);
        oneVbox.getChildren().add(noBox);

        HBox  sexBox = new HBox();
        sexBox.setSpacing(10);
        Label sexLab = new Label("性别");
        sexLab.setPrefWidth(width);
        ComboBox sexTF = new ComboBox<>();
        sexTF.setPrefWidth(widthMes);
        sexTF.getItems().addAll("男","女");
        sexTF.setId("sexTF");
        Label birthdayLab = new Label("出生日期");
        birthdayLab.setPrefWidth(width);
        DatePicker birthDayDP = new DatePicker();
        birthDayDP.setPrefWidth(widthMes);
        birthDayDP.setId("birthDayDP");
        sexBox.getChildren().addAll(sexLab,sexTF,birthdayLab,birthDayDP);
        oneVbox.getChildren().add(sexBox);

        HBox nativeBox = new HBox();
        nativeBox.setSpacing(10);
        Label nativeLab = new Label("籍贯");
        nativeLab.setPrefWidth(width);
        //籍贯信息的盒子
        HBox nativeHBox = new HBox();
        nativeHBox.setSpacing(10);
        //省
        ComboBox nativeTF = new ComboBox<>();
        nativeTF.setPrefWidth(widthMes/4);
        nativeTF.setId("nativeTF");
        nativeTF.setPromptText("省");
        //市
        ComboBox city = new ComboBox<>();
        city.setPrefWidth(widthMes/4);
        city.setId("city");
        city.setPromptText("市");
        //县
        ComboBox county = new ComboBox<>();
        county.setId("county");
        county.setPrefWidth(widthMes/2.6);
        county.setPromptText("县");
        nativeHBox.getChildren().addAll(nativeTF,city,county);

        Label nationLab = new Label("民族");
        nationLab.setPrefWidth(width);
        ComboBox nationTF = new ComboBox<>();
        nationTF.setPrefWidth(widthMes);
        nationTF.setId("nationTF");
        //调用方法56个民族封装方法
        Nation.nation(nationTF);

        nativeBox.getChildren().addAll(nativeLab,nativeHBox,nationLab,nationTF);
        oneVbox.getChildren().add(nativeBox);

        HBox birBox = new HBox();
        birBox.setSpacing(10);
        Label birthPlaceLab = new Label("出生地");
        birthPlaceLab.setPrefWidth(width);
        TextField birTF = new TextField();
        birTF.setPrefWidth(widthMes);
        birTF.setId("birTF");
        birBox.getChildren().addAll(birthPlaceLab,birTF);
        oneVbox.getChildren().add(birBox);

        HBox IDBox = new HBox();
        IDBox.setSpacing(10);
        Label IDTypeLab = new Label("证件类型");
        IDTypeLab.setPrefWidth(width);
        ComboBox IDTF = new ComboBox<>();
        IDTF.setPrefWidth(widthMes);
        IDTF.setId("IDTF");
        IDTF.getItems().addAll("身份证","士官证","学生证","驾驶证","护照","港澳通行证");
        IDTF.getSelectionModel().select(0);
        Label IDNoLab = new Label("证件号码");
        IDNoLab.setPrefWidth(width);
        TextField IDNoTF = new TextField();
        IDNoTF.setPrefWidth(widthMes);
        IDNoTF.setId("IDNoTF");
        IDBox.getChildren().addAll(IDTypeLab,IDTF,IDNoLab,IDNoTF);
        oneVbox.getChildren().add(IDBox);

        HBox maritalBox = new HBox();
        maritalBox.setSpacing(10);
        Label maritalStatusLab = new Label("婚姻状况");
        maritalStatusLab.setPrefWidth(width);
        ComboBox maritalCB = new ComboBox<>();
        maritalCB.setPrefWidth(widthMes);
        maritalCB.setId("maritalCB");
        maritalCB.getItems().addAll("未婚","已婚");
        maritalCB.getSelectionModel().select(0);
        Label politicsStatusLab = new Label("政治面貌");
        politicsStatusLab.setPrefWidth(width);
        ComboBox politicsCB = new ComboBox<>();
        politicsCB.setId("politicsCB");
        politicsCB.getItems().addAll("团员","党员","其他");
        maritalBox.getChildren().addAll(maritalStatusLab,maritalCB,politicsStatusLab,politicsCB);
        oneVbox.getChildren().add(maritalBox);



        VBox six = new VBox();
        six.setPrefWidth(300);
        Pane pane = new Pane();
        pane.setPrefHeight(100);
        pane.setMaxWidth(100);
        String url = "file:\\C:\\Users\\Administrator\\Pictures\\Saved Pictures\\666.jpeg";
        Image image = new Image(url);
        ImageView imageView = new ImageView(image);
        imageView.setFitWidth(100);
        imageView.setFitHeight(100);
        pane.getChildren().add(imageView);
        HBox hBox = new HBox();
        hBox.setPrefWidth(100);
        hBox.setSpacing(10);
        Label editPhoto = new Label("修改头像");
        editPhoto.setId("editPhoto");
        Label deletePhoto = new Label("删除");
        hBox.getChildren().addAll(editPhoto,deletePhoto);
        six.getChildren().addAll(pane,hBox);
        totalBox.getChildren().add(six);
        vBox1.getChildren().add(totalBox);

        //初始化编辑头像盒子
        initEditPhotoBox(editPhoto,six);
        deletePhoto.setCursor(Cursor.HAND);
        //删除头像
        BasicMesDeletePhoto basicMesDeletePhoto = new BasicMesDeletePhoto(six);
        deletePhoto.setOnMouseClicked(basicMesDeletePhoto);



        VBox two = new VBox();
        two.setPrefWidth(baseWidth);
        two.setSpacing(20);
        two.setPadding(new Insets(10));
        Integer twoWidth = baseWidth/8;
        Integer twoWidthMes = baseWidth/3;


        HBox englishBox = new HBox();
        englishBox.setSpacing(10);
        Label englishLab = new Label("英文姓名");
        englishLab.setPrefWidth(twoWidth);
        TextField englishTF = new TextField();
        englishTF.setPrefWidth(twoWidthMes);
        englishTF.setId("englishTF");
        Label nameSpellLab = new Label("姓名拼音");
        nameSpellLab.setPrefWidth(twoWidth);
        TextField nameSpellTF = new TextField();
        nameSpellTF.setPrefWidth(twoWidthMes-50);
        nameSpellTF.setId("nameSpellTF");
        englishBox.getChildren().addAll(englishLab,englishTF,nameSpellLab,nameSpellTF);
        two.getChildren().add(englishBox);


        HBox onceBox = new HBox();
        onceBox.setSpacing(10);
        Label onceNameLab = new Label("曾用名");
        onceNameLab.setPrefWidth(twoWidth);
        TextField onceNameTF = new TextField();
        onceNameTF.setPrefWidth(twoWidthMes);
        onceNameTF.setId("onceNameTF");
        Label faithLab = new Label("信仰宗教");
        faithLab.setPrefWidth(twoWidth);
        ComboBox faithCB = new ComboBox<>();
        faithCB.setId("faithCB");
        faithCB.getItems().addAll("无宗教信仰","佛教","伊斯兰教","基督教","其他");
        onceBox.getChildren().addAll(onceNameLab,onceNameTF,faithLab,faithCB);
        two.getChildren().add(onceBox);



        HBox nationalityBox = new HBox();
        nationalityBox.setSpacing(10);
        Label nationalityLab = new Label("国籍/地区");
        nationalityLab.setPrefWidth(twoWidth);
        TextField nationalityTF = new TextField();
        nationalityTF.setPrefWidth(twoWidthMes);
        nationalityTF.setId("nationalityTF");
        Label MacaoAndTaiwanLab = new Label("港澳台侨外");
        MacaoAndTaiwanLab.setPrefWidth(twoWidth);
        ToggleGroup MacaoAndTaiwanTG = new ToggleGroup();
        RadioButton yesRDB = new RadioButton("是");
        yesRDB.setId("yesRDB");
        RadioButton noRDB = new RadioButton("否");
        noRDB.setId("noRDB");
        MacaoAndTaiwanTG.getToggles().addAll(yesRDB,noRDB);
        nationalityBox.getChildren().addAll(nationalityLab,nationalityTF,MacaoAndTaiwanLab,yesRDB,noRDB);
        two.getChildren().add(nationalityBox);


        HBox healthBox = new HBox();
        healthBox.setSpacing(10);
        Label healthLab = new Label("健康状况");
        healthLab.setPrefWidth(twoWidth);
        ComboBox healthCB = new ComboBox<>();
        healthCB.setPrefWidth(twoWidthMes);
        healthCB.setId("healthCB");
        healthCB.getItems().addAll("健康或良好","一般或疾病","有慢性疾病","残疾");
        Label bloodLab = new Label("血型");
        bloodLab.setPrefWidth(twoWidth);
        ComboBox bloodCB = new ComboBox<>();
        bloodCB.setId("bloodCB");
        bloodCB.getItems().addAll("A型血","B型血","O型血","AB型血","其他");
        healthBox.getChildren().addAll(healthLab,healthCB,bloodLab,bloodCB);
        two.getChildren().add(healthBox);


        HBox idBox = new HBox();
        idBox.setSpacing(10);
        Label remarkLab = new Label("身份证有效期");
        remarkLab.setPrefWidth(twoWidth);
        VBox vBox = new VBox();
        vBox.setPrefWidth(twoWidthMes);
        vBox.setSpacing(10);
        DatePicker startDP = new DatePicker();
        startDP.setId("startDP");
        Label label = new Label("至");
        DatePicker stopDp = new DatePicker();
        stopDp.setId("stopDp");
        vBox.getChildren().addAll(startDP,label,stopDp);
        Label onlyChildLab = new Label("是否独生子女");
        onlyChildLab.setPrefWidth(twoWidth);
        ToggleGroup onlyChildTG = new ToggleGroup();
        RadioButton yesRDBtn = new RadioButton("是");
        yesRDBtn.setId("yesRDBtn");
        RadioButton noRDBtn = new RadioButton("否");
        noRDBtn.setId("noRDBtn");
        onlyChildTG.getToggles().addAll(yesRDBtn,noRDBtn);
        idBox.getChildren().addAll(remarkLab,vBox,onlyChildLab,yesRDBtn,noRDBtn);
        two.getChildren().add(idBox);
        vBox1.getChildren().add(two);

        children.set(3,vBox1);

        //取消按钮匿名内部类
        cancelBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                HBox editBox = new HBox();
                editBox.setPrefWidth(baseWidth);
                Insets insets1 = new Insets(10, 40, 10, 10);
                editBox.setPadding(insets1);
                editBox.setAlignment(Pos.CENTER_RIGHT);
                Button editBtn = new Button("💻编辑");
                editBox.getChildren().add(editBtn);
                //按钮样式
                BtnStyle.saveBtn(editBtn);
                //变成小手
                editBox.setCursor(Cursor.HAND);

                BasicMesEditAction basicMesEditAction = new BasicMesEditAction(totalVBox, one,basicVBox);
                editBtn.setOnAction(basicMesEditAction);

                children.set(2,editBox);
                children.set(3,BasicMesEditAction.basicVBox);




            }
        });


    }

    /**
     * 编辑头像
     * @param editPhoto
     */
    private void initEditPhotoBox(Label editPhoto,VBox six ) {
        //鼠标移动到label位置变颜色，变成小手图案
        editPhoto.addEventHandler(MouseEvent.MOUSE_ENTERED_TARGET, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                editPhoto.setStyle("-fx-text-fill: #00ccff");//变成红颜色
                editPhoto.setCursor(Cursor.HAND);//变成小手图案

            }
        });
        //鼠标退出时变回原来颜色
        editPhoto.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                editPhoto.setStyle("-fx-text-fill: #1c1b1b");
            }
        });
        //跳转事件
        editPhoto.addEventHandler(MouseEvent.MOUSE_CLICKED,new BasicMesAmendPhoto(six));
    }
}
